package com.dyw.handler;

import com.dyw.utils.ServiceTools;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @auther Devil
 * @create 2022-01-06-17:47
 */
public class MyIncationHandler implements InvocationHandler {
    private Object target;

    public MyIncationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //通过代理对象执行方法时,会调用执行这个invoke()
        Object res = null;
        if (method.getName().equals("doOther")) {
            res = method.invoke(target, args);
        } else if (method.getName().equals("doSome")) {
            //执行目标类的方法,通过method类实现
            ServiceTools.doLog();
            res = method.invoke(target, args);
            ServiceTools.doTrans();
            //目标方法执行结果
        }

        return res;
    }
}
